package com.zyb.learning.dataStructure.queue;

import java.util.Scanner;

/**
 * @ClassName Queue  使用数组模拟队列
 * @Author zhaoyb
 * @Date 2023/9/26
 * @Version 1.0
 */
public class ArrayQueueDemo {
    public static void main(String[] args) {
        //创建一个队列
        ArrayQueue arrayQueue = new ArrayQueue(3);
        char key = ' ';//接收输入
        Scanner scanner = new Scanner(System.in);
        boolean loop = true;
        while (loop){
            System.out.println("s(show):显示队列");
            System.out.println("e(exit):退出队列");
            System.out.println("g(get):从队列取出数据"); //出队
            System.out.println("a(add):添加数据到队列"); //入队
            System.out.println("h(head):查看队列头数据");
            key = scanner.next().charAt(0);
            switch (key){
                case 's':
                    arrayQueue.showQueue();
                    break;
                case 'e':
                    scanner.close();
                    loop = false;
                    break;
                case 'g':
                    try {
                        int result = arrayQueue.getQueue();
                        System.out.println("取出的数据是：" +result);
                    }catch (Exception e){
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'a':
                    System.out.println("请输入要添加的值");
                    int value = scanner.nextInt();
                    arrayQueue.addQueue(value);
                    break;
                case 'h':
                    try {
                        int headValue = arrayQueue.headQueue();
                        System.out.println("队列头数据是：" + headValue);
                    }catch (Exception e){
                        System.out.println(e.getMessage());
                    }
                    break;
                default:
                    break;
            }
        }
        System.out.println("程序退出");
    }

}

class ArrayQueue {
    private int maxSize; //数组最大容量
    private int front; //头指针
    private int rear; //尾指针
    private int[] arr; //模拟队列的数组

    /**
     * 创建队列的构造器
     *
     * @param arrMaxSize 队列大小
     */
    public ArrayQueue(int arrMaxSize) {
        maxSize = arrMaxSize;
        arr = new int[maxSize];
        front = -1;   //头指针 指向队列头的前一个位置
        rear = -1;
    }

    /**
     * 判断队列是否已经满了
     *
     * @return
     */
    public boolean isFull() {
        return rear == maxSize - 1;
    }

    /**
     * 判断队列是否空
     */
    public boolean isEmpty() {
        return rear == front;
    }

    public void addQueue(int n) {
        //判断是否满
        if (isFull()) {
            System.out.println("队列已经满了，不能添加");
            return;
        }
        rear++; //入队，尾指针加1
        arr[rear] = n;
    }

    /**
     * 出队列
     *
     * @return
     */
    public int getQueue() {
        //判空
        if (isEmpty()) {
            throw new IllegalArgumentException("队列已经空了，不能取数据");
        }

        front++; //头指针后移
        return arr[front];
    }


    //显示队列的所有数据
    public void showQueue() {
        //判空
        if (isEmpty()) {
            System.out.println("队列是空，没有数据！");
            return;
        }
        //以下遍历不太合适，应该是front+1 到rear
//            for (int i=0 ; i< arr.length;i++ ){
//                System.out.printf("arr[%d] = %d\n",i,arr[i]);
//            }

        for (int i = front + 1; i <= rear; i++) {
            System.out.printf("arr[%d] = %d\n", i, arr[i]);
        }
    }

    //显示队列的头数据，不出队列，仅仅展示
    public int headQueue() {
        if (isEmpty()) {
            throw new IllegalArgumentException("队列已经空了，获取不到数据");
        }

        return arr[front + 1];
    }
}
